package cn.lx.config;

import com.google.common.cache.CacheBuilder;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author lixin
 */
@Configuration
@EnableCaching
public class GuavaCacheConfig {

    /**
     * 配置缓存存活时间为 20 秒，缓存最大数目为 1000 个
     *
     * @return CacheManager
     */

    @Bean
    public CacheManager cacheManager() {
        GuavaCacheManager cacheManager = new GuavaCacheManager();
        cacheManager.setCacheBuilder(
                CacheBuilder.newBuilder().
                        expireAfterWrite(20, TimeUnit.SECONDS).
                        maximumSize(1000));
        return cacheManager;
    }
}
